package com.service.impl;

import com.dao.UserMapper;
import com.pojo.User;
import com.service.UserService;
import com.util.CheckData;
import com.util.CourseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    final private UserMapper userMapper;
    final private CheckData checkData;
    final private CourseUtil courseUtil;

    @Autowired(required = false)
    public UserServiceImpl(UserMapper userMapper, CheckData checkData, CourseUtil courseUtil){
        this.userMapper = userMapper;
        this.checkData = checkData;
        this.courseUtil = courseUtil;
    }

    @Override
    public List<User> findUserAll() {
        return userMapper.selectUserAll();
    }

    @Override
    public String logIn(String username, String password) {
        User user;
        if (checkData.isMobileNO(username)){
            try {
                user = userMapper.selectUSerByTel(username).get(0);
            } catch (Exception e){
                return "wrong tel";
            }
        } else if (checkData.checkEmail(username)){
            try {
                user = userMapper.selectUserByMail(username).get(0);
            } catch (Exception e){
                return "wrong mail";
            }
        } else {
            try {
                user = userMapper.selectUserById(username).get(0);
            } catch (Exception e){
                return "wrong id";
            }
        }

        if (user.getPassword().equals(password)){
            return "ok";
        } else {
            return "wrong password";
        }
    }

    @Override
    public String logOn(String mail, String password, String name, String school, String role) {
        User user = new User();
        String id = String.valueOf(userMapper.selectUserAll().size() + 1);
        if (checkData.checkEmail(mail)){
            user.setMail(mail);
            user.setPassword(password);
            user.setName(name);
            user.setSchool(school);
            user.setRole(role);
        } else if (checkData.isMobileNO(mail)){
            user.setTel(mail);
            user.setPassword(password);
            user.setName(name);
            user.setSchool(school);
            user.setRole(role);
        } else {
            return "invalid mail or tel";
        }

        try {
            user.setId(id);
            userMapper.addUser(user);
        } catch (Exception e){
            e.printStackTrace();
            return "wrong";
        }

        return "ok";
    }

    @Override
    public String changeRole(String role, String id) {
        User user;
        try {
            user = userMapper.selectUserById(id).get(0);
        } catch (Exception e){
            return "can not find user";
        }
        user.setRole(role);
        try {
            userMapper.updateUser(user);
        } catch (Exception e){
            return "update failed";
        }
        return "ok";
    }

    @Override
    public String changePassword(String id, String password) {
        User user;
        try {
            user = userMapper.selectUserById(id).get(0);
        } catch (Exception e){
            return "can not find user";
        }
        user.setPassword(password);
        try {
            userMapper.updateUser(user);
        } catch (Exception e){
            return "update failed";
        }
        return "ok";
    }

    @Override
    public String changeMail(String id, String mail) {
        User user;
        try {
            user = userMapper.selectUserById(id).get(0);
        } catch (Exception e){
            return "can not find user";
        }
        user.setMail(mail);
        try {
            userMapper.updateUser(user);
        } catch (Exception e){
            return "update failed";
        }
        return "ok";
    }

    @Override
    public String changeTel(String id, String tel) {
        User user;
        try {
            user = userMapper.selectUserById(id).get(0);
        } catch (Exception e){
            return "can not find user";
        }
        user.setTel(tel);
        try {
            userMapper.updateUser(user);
        } catch (Exception e){
            return "update failed";
        }
        return "ok";
    }

    @Override
    public List<String> getCourseListByStudentId(String id) {
        User user;
        try {
            user = userMapper.selectUserById(id).get(0);
        } catch (Exception e){
            return null;
        }

        List<String> courseList;
        try {
            courseList = courseUtil.string2List(user.getCourse());
        } catch (Exception e){
            return null;
        }

        if (courseList.get(0).equals("")){
            return null;
        }

        return courseList;
    }

    @Override
    public List<User> getUserByMailOrTel(String mail) {
        List<User> list;
        if (checkData.isMobileNO(mail)){
            try {
                list = userMapper.selectUSerByTel(mail);
            } catch (Exception e){
                return null;
            }
        } else if (checkData.checkEmail(mail)){
            try {
                list = userMapper.selectUserByMail(mail);
            } catch (Exception e){
                return null;
            }
        } else {
            return null;
        }

        return list;
    }
}
